package com.example.ksxtxkty.service;


import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Service;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


@Service
public class ApiService {


    protected final Logger logger = LogManager.getLogger(getClass());


        public  String test() throws Exception {
            String domain = "ztn-data.gdgov.cn";
            // 请求URL
            String url = "https://"+domain+":8581/GatewayMsg/http/api/proxy/invoke";

            // 请求头参数
            String paasId = "C90-44003530";
            String serviceId = "SJML20240002";
            String timestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            logger.info("当前时间搓："+ timestamp);
            String nonce = UUID.randomUUID().toString().replace("-", "");
            String secretKey = "72733c6eb8a14f238bb389987313ee49";

            // 计算签名
            String signatureData = timestamp + secretKey + nonce + timestamp;
            String signature = sha256(signatureData);

            // 创建HTTP连接
            HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("x-tif-paasid", paasId);
            connection.setRequestProperty("x-tif-serviceId", serviceId);
            connection.setRequestProperty("x-tif-timestamp", timestamp);
            connection.setRequestProperty("x-tif-nonce", nonce);
            connection.setRequestProperty("x-tif-signature", signature);

            // POST请求且有请求体
             connection.setDoOutput(true);
             String requestBody = "AAB069=广东一方制药有限公司&loginId=sjb";
             try (OutputStream os = connection.getOutputStream()) {
                 byte[] input = requestBody.getBytes(StandardCharsets.UTF_8);
                 os.write(input, 0, input.length);
             }

            // 获取响应
            int responseCode = connection.getResponseCode();
            logger.info("Response Code: " + responseCode);

            try (BufferedReader in = new BufferedReader(
                    new InputStreamReader(connection.getInputStream()))) {
                String inputLine;
                StringBuilder response = new StringBuilder();
                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                logger.info("Response: " + response.toString());
            }
            return connection.getResponseMessage();
        }

        private static String sha256(String data) throws Exception {
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(data.getBytes(StandardCharsets.UTF_8));
            StringBuilder hexString = new StringBuilder();
            for (byte b : hash) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) hexString.append('0');
                hexString.append(hex);
            }
            return hexString.toString();
        }







}
